package br.edu.curso.java.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.edu.curso.java.BO.LoginBO;
import br.edu.curso.java.VO.UsuarioVO;
import br.edu.curso.java.utils.AcademicNetException;

/**
 * Servlet implementation class LoginServlet esse ta no master e Git
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		encerrarSessao(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**
		UsuarioVO usuarioLogado = new UsuarioVO();
		
		request.getSession().setAttribute("UsuarioLogado", usuarioLogado);
		RequestDispatcher rd = request.getRequestDispatcher("principal.jsp");
		
		rd.forward(request, response);
		
		*/

			efetuarLogin(request, response);	
	
				
	}
	
	private void efetuarLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String  login = (String) request.getParameter("txtLogin");
		String  senha = (String) request.getParameter("txtSenha");

		LoginBO loginBO = new LoginBO();
		
		try {
			UsuarioVO usuarioLogado = loginBO.EfetuarLogin(login, senha);
			
			request.getSession().setAttribute("UsuarioLogado", usuarioLogado);
			
			RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/Principal.jsp");
	
			rd.forward(request, response);
			
		} catch (AcademicNetException eAcademic) {

			request.setAttribute("msg", eAcademic.getMessage());
			
			RequestDispatcher rd = request.getRequestDispatcher("Login.jsp");
	
			rd.forward(request, response);

		} catch (Exception e) {
				
			request.setAttribute("ex", e);
			
			RequestDispatcher rd = request.getRequestDispatcher("Erro.jsp");
			
			rd.forward(request, response);
		
		}
		
	}

	private void encerrarSessao(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		 HttpSession sessao = request.getSession();		 
		 
		 sessao.invalidate();
		 
		RequestDispatcher rd = request.getRequestDispatcher("Login.jsp");
	
		rd.forward(request, response);
		
	}
}
